package hkr.se1.cyvasse;

import Game.Game;
import android.content.ClipData;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnTouchListener;

public class MyTouchListener implements OnTouchListener {
	Game game;

	public MyTouchListener(Game game) {
		this.game = game;
	}

	@Override
	public boolean onTouch(View view, MotionEvent motionEvent) {
		if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
			ClipData data = ClipData.newPlainText("", "");
			TileView v = (TileView) view;
			// Set the dragshadow to the lifted view
			DragShadowBuilder shadowBuilder;
			// Start the drag

			// Calculate the row of the Tile that the Piece was dropped in.
			int row = v.getId() / 8;

			// Calculate the column of the Tile that the Piece was dropped
			// in.
			int col = v.getId() % 8;
			switch (game.getSetup()) {
			case Game.TERRAIN_SETUP:
				if (game.checkTerrainDrop(row, col)) {
					shadowBuilder = new View.DragShadowBuilder(v.getTileView());
					view.startDrag(data, shadowBuilder, view, 0);
				}
				break;
			case Game.PIECE_SETUP:
				if (game.viablePiece(row, col)) {
					shadowBuilder = new View.DragShadowBuilder(v.getPieceView());
					view.startDrag(data, shadowBuilder, view, 0);
				}
				break;
			case Game.RUN_GAME:
				shadowBuilder = new View.DragShadowBuilder(v.getPieceView());
				view.startDrag(data, shadowBuilder, view, 0);
				break;
			}
			return true;
		} else {
			return false;
		}
	}

}
